package com.netbetter.langzheng.commons.gears.utils;

import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.netbetter.langzheng.commons.gears.config.DataSourceConfig;
import com.netbetter.langzheng.commons.gears.dto.ParametersDTO;

public class UrlUtils {

  private static String runtimeStatus = DataSourceConfig.getRuntimeStatus();

  private static boolean isLocal() {
    if ("local".equalsIgnoreCase(runtimeStatus)) {
      return true;
    }
    return false;
  }

  /**
   * 分析parameterDTO获得远程或本地测试数据的url
   * 
   * @param dto
   * @return
   */
  public static String getUrl(ParametersDTO dto) {
    if (isLocal()) {
      return dto.getRootUrl() + ".js";
    } else {
      StringBuffer buffer = new StringBuffer();
      Iterator<String> iter = dto.getParameterMap().keySet().iterator();
      while (iter.hasNext()) {
        String key = (String) iter.next();
        buffer.append("&").append(key).append("=").append(
            dto.getParameterMap().get(key).toString());
      }
      if (buffer.toString().length() > 0) {
        return GWT.getHostPageBaseURL() + dto.getRootUrl() + "?1=1"
            + buffer.toString();
      } else {
        return GWT.getHostPageBaseURL() + dto.getRootUrl();
      }
    }
  }
}
